package com.lzk.pet.service.imp;


import com.lzk.basic.service.impl.BaseServiceImpl;
import com.lzk.basic.util.JsonResult;
import com.lzk.pet.domain.Pet;
import com.lzk.pet.domain.PetDetail;
import com.lzk.pet.mapper.PetDetailMapper;
import com.lzk.pet.mapper.PetMapper;
import com.lzk.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;


@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private PetMapper petMapper;

    @Transactional
    @Override
    public void save(Pet pet) {
        super.save(pet);
        //获取宠物详情
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail!=null){
            petDetail.setPet_id(pet.getId());
            //添加宠物详情
            petDetailMapper.save(petDetail);
        }
    }

    @Transactional
    @Override
    public void update(Pet pet) {
        super.update(pet);
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail!=null){
            //之前在分页查询已经将id查询
            petDetailMapper.update(petDetail);
        }
    }

    @Transactional
    @Override
    public void remove(Long id) {
        super.remove(id);
        petDetailMapper.removePetDetailByPetId(id);
    }

    /**
     * 下架业务实现
     * @param ids
     * @return
     */
    @Override
    public JsonResult offSale(List<Long> ids) {
        Date date = new Date();
        petMapper.offSale(ids,date);
        return JsonResult.me();
    }

    /**
     * 上架业务实现
     * @param ids
     * @return
     */
    @Override
    public JsonResult onSale(List<Long> ids) {
        Date date = new Date();
        petMapper.onSale(ids,date);
        return JsonResult.me();
    }
}
